<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// +----------------------------------------------------------------------
// | 网站配置模型
// +----------------------------------------------------------------------
// | Copyright (c) 2015
// +----------------------------------------------------------------------
// | Author: baiping 125618036@qq.com http://www.webipcode.com
// +----------------------------------------------------------------------
// | 2015-6-26下午2:43:52
// +----------------------------------------------------------------------

class Config_model extends CI_Model{
	
	public function __construct(){
		parent::__construct();
		$this->load->helper('common');
	}
	
	/**
	 * 获取数据
	 * @param array $where 获取条件 
	 * @param string $field 获取字段
	 * @param string $limit 获取数量
	 * @param string $order 排序
	 * @author Baip 
	 * @deprecated:limit的传值为（每页记录数,偏移数）生成的sql语句为 limit(偏移数,记录数)
	 */
	public function getData($where=array(), $field="id,name,group,type", $limit='', $offset=0,  $order="sort ASC"){
		$res = $this->db->select($field)->where($where)->order_by($order)->limit($limit, $offset)->get('config');
		return $res->result();
	}
	
	/**
	 * 获取数据部数
	 * @param unknown_type $where
	 * @param unknown_type $field
	 */
	public function getCount($where=array(), $field="id"){
		$count =  $this->db->where($where)->select($field)->count_all_results('config');
		return $count;
		
	}
	
	/**
	 * 添加数据
	 */
	public function addData(){
		$name  = $this->input->post('name',true);//配置标识
		$title = $this->input->post('title',true);//配置标题
		$sort  = intval( $this->input->post('sort',true) );//配置排序
		$group = intval( $this->input->post('group',true) );//配置分组
		$type  = intval( $this->input->post('type',true) );//配置类型
		$value = $this->input->post('value',true) ;//配置值
		$extra = $this->input->post('extra',true) ;//配置项
		$remark= $this->input->post('remark',true);//配置类型
		
		$data_arr['name'] = strtoupper( $name );
		$data_arr['title'] = $title;
		$data_arr['sort'] = $sort;
		$data_arr['group'] = $group;
		$data_arr['type'] = $type;
		$data_arr['value'] = $value;
		$data_arr['extra'] = $extra;
		$data_arr['remark'] = $remark;
		$data_arr['status'] = 1;
		$data_arr['create_time'] = time();
		$data_arr['update_time'] = time();
		
		$query = $this->db->insert('config', $data_arr);
		return $this->db->insert_id();
		
	}
	/**
	 * 更新数据
	 */
	public function updateData($where){
		$name  = $this->input->post('name',true);//配置标识
		$title = $this->input->post('title',true);//配置标题
		$sort  = intval( $this->input->post('sort',true) );//配置排序
		$group = intval( $this->input->post('group',true) );//配置分组
		$type  = intval( $this->input->post('type',true) );//配置类型
		$value = $this->input->post('value',true) ;//配置值
		$extra = $this->input->post('extra',true) ;//配置项
		$remark= $this->input->post('remark',true);//配置类型
		
		$data_arr['name'] = strtoupper( $name );
		$data_arr['title'] = $title;
		$data_arr['sort'] = $sort;
		$data_arr['group'] = $group;
		$data_arr['type'] = $type;
		$data_arr['value'] = $value;
		$data_arr['extra'] = $extra;
		$data_arr['remark'] = $remark;
		$data_arr['status'] = 1;
		$data_arr['create_time'] = time();
		$data_arr['update_time'] = time();
		
		$query = $this->db->where($where)->update('config', $data_arr);
		return $query;
		
	}
	
	/**
	 * 删除数据
	 * @param array $where 删除条件
	 */
	public function deleteData($field, $del_data){
		if ( is_array( $del_data ) ){
			$this->db->where_in($field, $del_data);
		}else{
			$this->db->where($field, $del_data);
		}
		$query = $this->db->delete('config');
		return $query;
	}
	
	/**
	 * 获取单条数据详情
	 * @param string $field 条件字段
	 * @param  string/array $where_data　条件数据
	 * @param string $field 要读取的字段
	 * @param string $order 排序
	 */
	public function getInfo($w_field, $where_data, $field="*", $order="id DESC"){
		if ( empty( $w_field ) || empty( $where_data ) ){
			return false;
		}
		
		if ( is_array( $where_data ) ){
			$this->db->where_in($w_field, $where_data);
		}else{
			$this->db->where($w_field, $where_data);
		}
		
		$res = $this->db->select($field)->limit(1)->order_by($order)->get('config');
		if ( $res->num_rows() > 0 ){
			return $res->row();
		}else{
			return false;
		}
	}
	
	/**
	 * 更新数据
	 */
	public function updateDataConfig(){
		$config  = $this->input->post('config',true);//配置标识
		foreach ( $config as $key => $val ){
			$map = array('name'=> $key);
			$data = array('value'=> $val);
			$this->db->where($map)->update('config',$data);
		}
		//TODO 更新缓存
		$this->load->driver('cache');//使用缓存
		$this->cache->delete('config_data');//清除缓存
		return true;
	}	
}